Excel Program: ON OFF.XLS

Implementation Select: Implementation
Operation Select: Exel Program: onoff.xls

Target "CALCULATE"

Private Sub Calculate1_Click()
ikol = 5
Do
    ikol = ikol + 1
Loop Until Cells(1, ikol).Value = "total" Or ikol = 150
ikolend = ikol - 1                ' column to select all - standard 12
If ikol = 150 Then ikolend = 12   'no "total" found
ikolsel = Selection.Column        'selection column
irowsel = Selection.Row           'not used

If ikolsel < 3 Then ikolsel = 3     'result
If ikolsel > ikolend Then ikolsel = ikolend   'result
If ikolsel = ikolend Then istr = 3 Else istr = ikolsel

For jkol = istr To ikolsel
    Cells(1, jkol).Value = "bit"
    Cells(1, jkol).Select
    time1 = Time
    ' Synchronisation Loop
    Do
        DoEvents
    Loop Until time1 <> Time
    pval = 0: Direction = 1: ikol = 1
    For irow = 1 To 40                 ' maximum 64 for 1 second
        time1 = Timer
        ' Debug.Print Timer
        cnt = 0
        Do
            cnt = cnt + 1
            DoEvents
        Loop Until Timer <> time1
        Cells(irow + 1, ikol).Value = time1
        Cells(irow + 1, ikol + 1).Value = cnt
        deltapv = (cnt Mod 10) + 1           ' 1 -- 10
        If Direction = 1 Then
            pval = pval + deltapv
            If pval > 2 Then Direction = 0
        Else
            pval = pval - deltapv
            If pval < -2 Then Direction = 1
        End If
        Cells(irow + 1, jkol).Value = pval
    Next irow

    If pval > 0 Then pval = 1 Else pval = 0    ' advantage 0
    Cells(1, jkol).Value = "bit  " + Str(Direction)
    Cells(irow + 1, jkol).Value = ""
Next jkol

End Sub

Target "TEST"

Private Sub Test_Click()

ikol = 5
onval = 2
Do
    ikol = ikol + 1
Loop Until Cells(1, ikol).Value = "total" Or ikol = 150
ikolend = ikol - 1                ' column to select all - standard 12
If ikol = 150 Then ikolend = 12   'no "total" found
ikolsel = Selection.Column        'selection column
irowsel = Selection.Row           'not used

If ikolsel < 3 Then ikolsel = 3     'result
If ikolsel > ikolend Then ikolsel = ikolend   'result
If ikolsel = ikolend Then istr = 3 Else istr = ikolsel
    Mesg$ = "On value = " + Str$(onval)
       
    on1$ = InputBox(Mesg$, "On value", , , "2")
    If on1$ <> "" Then onval = Val(on1$)

For jkol = istr To ikolsel
    Cells(1, jkol).Value = ""
    Cells(1, jkol).Select
    time1 = Time
    ' Synchronisation Loop
    Do
        DoEvents
    Loop Until time1 <> Time
    pval = 0: Direction = 1: ikol = 1
    corr = 0
    corr1 = 0
    For irow = 1 To 40                 ' maximum 64 for 1 second
        time1 = Timer
        ' Debug.Print Timer
        cnt = 0
        Do
            cnt = cnt + 1
            DoEvents
        Loop Until Timer <> time1
        Cells(irow + 1, ikol).Value = time1
        Cells(irow + 1, ikol + 1).Value = cnt
        deltapv = (cnt Mod 10) + 1           ' 1 -- 10
        If Direction = 1 Then
            pval = pval + deltapv
            If pval > onval Then Direction = 0
        Else
            pval = pval - deltapv
            If pval < -onval Then Direction = 1
        End If
        temp = Direction
        If temp = 0 Then temp = -1
        Cells(irow + 1, jkol).Value = temp
        If jkol > 3 Then
            temp1 = Cells(irow + 1, jkol - 1).Value
            corr = corr + temp * temp1
        End If
        If irow > 1 Then
            temp1 = Cells(irow, jkol).Value         ' previous value
            corr1 = corr1 + temp * temp1
        End If
    Next irow

    If pval > 0 Then pval = 1 Else pval = 0    ' advantage 0
    ' Cells(1, jkol).Value = "bit  " + Str(Direction)
    Cells(1, jkol).Value = corr
    Cells(irow + 1, jkol).Value = corr1

Next jkol

End Sub


Target "NIST"

Public Sub NIST()
Test = 0
    Mesg$ = "Test ? (-2 to 32)"
    on1$ = InputBox(Mesg$, "Test", 0)
    If on1$ <> "" Then Test = Val(on1$)
    Cells(3, 2).Value = ""
    Cells(3, 3).Value = ""
    Cells(4, 2).Value = Test
    For irow = 4 To 11
        For ikol = 3 To 5
            Cells(irow, ikol).Value = ""
        Next ikol
    Next irow
    Cells(5, 1).Value = "p"
    Cells(5, 2).Value = ""
    Cells(6, 1).Value = ""
    Cells(6, 2).Value = ""
    
    For i = 1 To 256: Cells(1, i).Value = "": Cells(2, i).Value = "": Next i

Select Case Test
Case 0, -1, -2
    Standard Test
Case 1
    Eps = "1011010101"      ' Test 2.1.4    Error Test 2.14
Case 2
    Eps = "0110011010"      ' Test 2.2.4    No Error
Case 3
    Eps = "1001101011"      ' Test 2.3.4    No Error
Case 4                      ' Test 2.4.8    Error 2.10  Test 3
    Eps = "11001100000101010110110001001100111000000000001001"
    Eps = Eps + "00110101010001000100111101011010000000110101111100"
    Eps = Eps + "1100111001101101100010110010"
Case 5
    Eps = "01011001001010101101"      ' Test 2.5.4  Error 2.10
Case 6
    Eps = "1001010011"                ' Test 2.6.4  No error
Case 7
    Eps = "10100100101110010110"      ' Test 2.7.4  Error 2.10
Case 8                                ' Test 2.8.4  Error 2.10 2.14
    Eps = "10111011110010110100011100101110111110000101101001"
    Eps = "10111011110010110110011100101110111110000101101001"
Case 9
    Eps = "01011010011101010111"      ' Test 2.9.4  Error 2.10
Case 10
    Eps = "1101011110001"             ' Test 2.10.4 Error 2.10 & 2.14
    Eps = "1101011110001110101001111100111100100010001110100010"   ' Test 2.10.4 Error 2.10 & 2.14
Case 11
    Eps = "0011011101"                ' Test 2.11.4
Case 12
    Eps = "0100110101"                ' Test 2.12.4
Case 13
    Eps = "1011010111"      ' Test 2.13.4   Error 2.14
Case 14
    Eps = "0110110101"      ' Test 2.14.4  No error
Case 15
    Eps = "0110110101"      ' Test 2.15.4  No Error
Case 16
                             ' Test 2.1.8  Test 2.2.8  Test 2.3.8  Test 2.6.8  Test 2.12.8 Test 2.13.8
    Eps = "11001001000011111101101010100010001000010110100011"
    Eps = Eps + "00001000110100110001001100011001100010100010111000"
Case 17                      ' n = 256  Error  Test 2.10  Test 3
    Eps = "0011010101100100101011011010101100001110101001110011110110111000000011000100110101000100100011110110010001001110111110000010011110110100101010010111111111010100001110100011101000110101101100011001110101111011000101101011100000000000011001111011110101110111"
Case 18                      ' n = 256  Error  2.14
    Eps = "1100000110101111000110000000110101001011000011101010110101111010100111110000101111001110111011011001011010001101001011001110001010010001000010011011001101010011011100000001010001000101001110100110100110010001100110001010110111101100011111000011010111110000"
Case 19                      ' n = 256  Error  2.14
    Eps = "0011001001101010000110111100110110110111110111011111101010001010011001000001011101111001110111011100010010100111011111100000000101111011111100011100000011100100100100111110011000010011110111111010011010100011000001000101110001111110000111001110110000101101"
Case 20                      ' Error  2.11 & 2.12  Test 3
    Eps = "010011000111000011110000011111000000111111000000011111110000000011111111"
Case 21                      ' Error  2.7, 2.8, 2.11, 2.12 & 2.14 Test 3  3.1
    Eps = "010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101"
Case 22                      ' Error  2.7 2.8  2.11, 2.12 & 2.14 Test 3
    Eps = "001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011"
Case 23                      ' Error  2.11 & 2.12   Test 3 3.1
    Eps = "000011110000111100001111000011110000111100001111000011110000111100001111000011110000111100001111"
Case 24                      ' n = 100  Error  2.8, 2.11, 2.12 & 2.14 Test 3  3.1
    Eps = "0000011111000001111100000111110000011111000001111100000111110000011111000001111100000111110000011111"
Case 25                      ' n = 100  Error 7 & 14
    Eps = "1110111101101111100011101010101101011111110101111010111100110000100000111001011000000011001001011111"
Case Is = 26                 ' n = 8   No error
    Eps = "01001101"
Case Is = 27                 ' n = 22  No error
    Eps = "0100110100011101001101"
Case Is = 28                 ' n = 52  Error 2.8 2.12
    Eps = "0100110100011101001101000011110100110100011101001101"
Case Is = 29                 ' n = 114  Error 2.12 2.14
    Eps = "010011010001110100110100001111010011010001110100110100000111110100110100011101001101000011110100110100011101001101"
Case Is = 30                 ' n = 240  Error 2.8, 2.12 2.14
    Eps = "010011010001110100110100001111010011010001110100110100000111110100110100011101001101000011110100110100011101001101"
    Eps = Eps + "111111000000010011010001110100110100001111010011010001110100110100000111110100110100011101001101000011110100110100011101001101"
Case Is = 31  'No Error    E = 2.71828182845904523536028747135266249775724709369995   Error 2.12
    Eps = "101110011111111000001101010010010010110000011111000110111100101010001100100111001011011101110110101100100001111010100001110010001010100101000000000110010101100010001011"
Case Is = 32  'Error 2.2  Test 3  pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706    length = 100
    Eps = "1010011111101111010011000110011100001011110011000001010000000011110010000100100000000011000011000010100101110110101101110111111100100111100110100000010100001101111111001001111"
Case Else
    End
End Select
    Pi = 4 * Atn(1)
    pi2 = Pi * 2
Debug.Print "Nist"
Debug.Print "Nist Test ="; Test
Debug.Print "Nist Eps ="; Eps

errorc = 0
pminx = 1

Name$ = "Test 2.1"
Debug.Print "Test 2.1 Frequency (Monobit) Test page 2.2"
Sn = 0
Vnobs = 0
n1 = Len(Eps)
Cells(3, 2).Value = n1
MaxSn = 0                                             ' Test 2.13
jrw = 0                                               ' Test 2.14   Sn = 0 zero crossings
For i = 1 To n1
    a$ = Mid$(Eps, i, 1)
    If a$ = "1" Then in1(i) = 1 Else in1(i) = -1
    If i < 256 And Test > 0 Then Cells(1, i).Value = a$: Cells(2, i).Value = ""
    Sn = Sn + in1(i)
    If Sn = 0 Then jrw = jrw + 1                      ' Test 2.14
    If Abs(Sn) > MaxSn Then MaxSn = Abs(Sn)           ' Test 2.13
Next i
jrw = jrw + 1                                         ' Test 2.14  At end Sn = 0
Sobs = Abs(Sn) / Sqr(n1)

Debug.Print "Nist n ="; n1; "Sn ="; Sn; "Sobs ="; Sobs

z = Sobs / Sqr(2)
z = Abs(z)
Erfc z, p

Debug.Print "Nist z ="; z; "p = Erfc ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If Test = 1 Then Cells(5, 2).Value = p

Name$ = "Test 2.2"
Debug.Print "Test 2.2 Frequency Test within a Block page 2.4 "
mm = Int(Sqr(n1)): nn = mm
Debug.Print "Nist M = "; mm; "N = "; nn;

For i = 1 To nn
   pic = 0
   For j = 1 To mm
      pnt = (i - 1) * mm + j
      If in1(pnt) = 1 Then pic = pic + 1
   Next j
   pix(i) = pic / mm
   ' Debug.Print i, pic, pic / M
Next i
kappa2 = 0
For i = 1 To nn
    kappa2 = kappa2 + (pix(i) - 0.5) * (pix(i) - 0.5)
Next i
kappa2 = kappa2 * 4 * mm
Debug.Print "Kappa2 ="; kappa2;

' incomplete gamma function
igamc nn / 2, kappa2 / 2, p

Debug.Print "igamc = p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 2 Then Cells(5, 2).Value = p

Name$ = "Test 2.3"
Debug.Print "Test 2.3 Runs Test page 2.5"
' Test all the one's
pictot = 0
For i = 1 To n1
    If in1(i) = 1 Then pictot = pictot + 1
Next i
picavr = pictot / n1
tau = 2 / Sqr(n1)
Debug.Print "Nist n ="; n1; "Pi ="; picavr; "Tau ="; tau
If Abs(picavr - 0.5) < tau Then Debug.Print "No runs test" Else Debug.Print "Runs test applicable"

Vnobs = 0
n1 = Len(Eps)
For i = 1 To n1 - 1
    a1$ = Mid$(Eps, i, 1)
    a2$ = Mid$(Eps, i + 1, 1)
    If a1$ <> a2$ Then Vnobs = Vnobs + 1
Next i
Vnobs = Vnobs + 1
z = (Vnobs - 2 * n1 * picavr * (1 - picavr)) / (2 * (Sqr(2 * n1)) * picavr * (1 - picavr))
z = Abs(z)
Erfc z, p

Debug.Print "Nist Vnobs ="; Vnobs; "z ="; z; "p = Erfc ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 3 Then Cells(5, 2).Value = p

Name$ = "Test 2.4"
Debug.Print "Test 2.4 Test for the Longest Run of Ones in a Block page 2.7 "
    If n1 >= 750000 Then
        mm = 1000: kk = 6: nn = 75: vstr = 10: vmax = 16
        prob(0) = 0.0882: prob(1) = 0.2092: prob(2) = 0.2483: prob(3) = 0.1933: prob(4) = 0.1208: prob(5) = 0.0675: prob(6) = 0.0727
    Else
        If n1 >= 6272 Then
            mm = 128: kk = 5: nn = 49: vstr = 4: vmax = 9
            prob(0) = 0.1174: prob(1) = 0.243: prob(2) = 0.2493: prob(3) = 0.1752: prob(4) = 0.1027: prob(5) = 0.1124
        Else
            If n1 >= 128 Then
                mm = 8: kk = 3: nn = Int(n1 / 8): vstr = 1: vmax = 4
                prob(0) = 0.2148: prob(1) = 0.3672: prob(2) = 0.2305: prob(3) = 0.1875
            Else
                mm = 0: kk = 0: nn = 0
            End If
        End If
    End If
Debug.Print "Nist M ="; mm; "K ="; kk; "N ="; nn

If mm = 0 Then GoTo Me25

For i1 = 0 To 6: v(i1) = 0: Next i1
For i = 1 To n1 Step mm
   a$ = Mid$(Eps, i, mm)
   Run1 = 0
   maxrun = 0
   For j = i To i + mm - 1
      a1$ = Mid$(Eps, j, 1)
      If a1$ = "1" Then
          Run1 = Run1 + 1
      Else
          If Run1 > maxrun Then maxrun = Run1
          Run1 = 0
      End If
   Next j
   If Run1 > maxrun Then maxrun = Run1
   vj = maxrun - vstr
   If vj < 0 Then vj = 0
   If vj > vmax - vstr Then vj = vmax - vstr
   v(vj) = v(vj) + 1
   If n1 < 256 Then Debug.Print a$; "Maxrun"; maxrun; vstr; vj; v(vj)
Next i
For i1 = 0 To vmax - vstr
   Debug.Print i1; "v"; v(i1); "prob"; prob(i1)
Next i1
X2obs = 0
For i = 0 To kk
    X2obs = X2obs + (v(i) - nn * prob(i)) * (v(i) - nn * prob(i)) / (nn * prob(i))
Next i
Debug.Print "X2obs"; X2obs
' incomplete gamma function
igamc kk / 2, X2obs / 2, p
Debug.Print "igamc = p ="; p
If p > 0.01 Then Debug.Print "Random" Else Debug.Print "Non Random": errorc = errorc + 1
If p < 0.01 Then Cells(4, 3).Value = Name$: Cells(4, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 4 Then Cells(5, 2).Value = p

Me25:
Name$ = "Test 2.5"
Debug.Print "Test 2.5 Binary Matrix Rank Test page 2.10 "
mm = Int(Sqr(n1 / 2)): Q = mm
nn = Int(n1 / (mm * Q))
For y = 1 To Q
    For i = 1 To nn
        For x = 1 To mm
             pnt = (y - 1) * mm + x + (i - 1) * mm * Q
             If in1(pnt) = 1 Then a$ = " 1" Else a$ = " 0"
             If n1 < 256 Then Debug.Print a$;
        Next x
    Next i
    If n1 < 256 Then Debug.Print ""
Next y

FM = 0
FM1 = 0
displayc = 0   ' Rank - no display
For i = 1 To nn
    For j = 1 To mm * Q
        pnt = j + (i - 1) * mm * Q
        in2(j) = in1(pnt)
        If in2(j) = -1 Then in2(j) = 0
    Next j
    rank in2(), Q, rank1
    Debug.Print "rank"; rank1
    If rank1 = Q Then FM = FM + 1
    If rank1 = Q - 1 Then FM1 = FM1 + 1
Next i

Q2obs = 0
Q2obs = Q2obs + (FM - 0.2888 * nn) * (FM - 0.2888 * nn) / (0.2888 * nn)
Q2obs = Q2obs + (FM1 - 0.5776 * nn) * (FM1 - 0.5776 * nn) / (0.5776 * nn)
Q2obs = Q2obs + (nn - FM - FM1 - 0.1336 * nn) * (nn - FM - FM1 - 0.1336 * nn) / (0.1336 * nn)

p = Exp(Q2obs / 2)
Debug.Print "Nist FM ="; FM; "FM-1 ="; FM1; "Q2obs ="; Q2obs; "p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If Test = 5 Then Cells(5, 2).Value = p

Name$ = "Test 2.6"
Debug.Print "Test 2.6 Discrete Fourier Transform (Spectral) Test page 2.12 "
R2 = 0: Low = 0: High = 0
T = Sqr(Log(1 / 0.05) * n1)
 ' Consider only the half of n
For j = 1 To n1 / 2 - 1
    fre(j) = 0: fim(j) = 0
    For k = 1 To n1
        ' fre(j) = fre(j) + in1(k) * Cos(pi2 * k * j / n1)  'real
        ' fim(j) = fim(j) + in1(k) * Sin(pi2 * k * j / n1)  'imaginar
        re = in1(k) * Cos(pi2 * k * j / n1)
        im = in1(k) * Sin(pi2 * k * j / n1)
        If k = 1 Then
            re1 = re
            im1 = re
         Else
            re1 = re * re1 - im * im1
            im1 = re * im1 + im * re1
         End If
    Next k
    fre(j) = re1: fim(j) = im1
    R2 = Sqr(fre(j) * fre(j) + fim(j) * fim(j))
    If R2 < T Then
        Low = Low + 1
        If Test = 6 Then Debug.Print j; in1(j); "High"; High; "R"; R2
    Else
        High = High + 1
        Debug.Print j; in1(j); "High"; High; "R"; R2
    End If
Next j
nn0 = 0.95 * n1 / 2
nn1 = Low
d = (nn1 - nn0) / Sqr(n1 * 0.95 * 0.05 / 4)

z = Abs(d) / Sqr(2)
Erfc z, p

Debug.Print "Nist T ="; T; "n ="; n1; "N0 ="; nn0; "N1 ="; nn1; "d ="; d
Debug.Print "Nist z ="; z; "p = Erfc ="; p; "T max ="; 100 * Low / ((n1 / 2) - 1); "%"
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 6 Then Cells(5, 2).Value = p

Name$ = "Test 2.7"
Debug.Print "Test 2.7 Non-overlapping Template Matching Test page 2.14 "
nn = 2
mm = Int(n1 / nn)
Debug.Print "Nist M ="; mm; "n = "; n1; "N ="; nn
Mask = "001"
l = Len(Mask): m = l
For i = 1 To nn
   wn = 0
   For j = 1 To mm + 1 - l
      pos = j + (i - 1) * mm
      If Mid$(Eps, pos, l) = Mask Then wn = wn + 1: j = j + l - 1
   Next j
   Debug.Print Mid$(Eps, (i - 1) * mm + 1, mm); " i"; i; "wn"; wn
   If i = 1 Then w1 = wn
   If i = 2 Then w2 = wn
Next i
mu = (mm - m + 1) / 2 ^ m
sigma2 = mm * (1 / 2 ^ m - (2 * m - 1) / 4 ^ m)
X2obs = ((w1 - mu) * (w1 - mu) + (w2 - mu) * (w2 - mu)) / sigma2
Debug.Print "mu ="; mu; "Sigma2 ="; sigma2; "X2(obs) ="; X2obs; "N ="; nn
' incomplete gamma function
igamc nn / 2, X2obs / 2, p
Debug.Print "igamc = p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 7 Then Cells(5, 2).Value = p

Name$ = "Test 2.8"
Debug.Print "Test 2.8 Overlapping Template Matching Test page 2.17 "
If n1 < 100 Then
   m = 2: mm = 10  ' n1 = 20
   Mask = "11"
Else
   If n1 < 1000 Then
       m = 3: mm = 20  ' n1 = 100
       Mask = "111"
   Else
       m = 4: mm = 36  ' n1 = 1000
       Mask = "1111"
   End If
End If
kk = 5
' nn = 5
nn = Int(n1 / mm)
Debug.Print "Nist K ="; kk; "M ="; mm; "n = "; n1; "N ="; nn;
' m = Log(mm) / Log(2)
labda = (mm - m + 1) / 2 ^ m: eta = labda / 2
Debug.Print "labda = "; labda; "; eta = "; eta

If nn = 0 Then GoTo Me29
For i = 0 To kk: nu(i) = 0: Next i
l = Len(Mask): m = l
If mm < m Then GoTo Me29
For i = 1 To nn
   wn = 0
   For j = 1 To mm + 1 - m
      pos = j + (i - 1) * mm
      If Mid$(Eps, pos, m) = Mask Then wn = wn + 1
   Next j
   Debug.Print Mid$(Eps, (i - 1) * mm + 1, mm); " i"; i; "wn"; wn
   If wn > 5 Then wn = 5
   nu(wn) = nu(wn) + 1
Next i
For i = 0 To kk: Debug.Print "i ="; i; "nu"; nu(i); ",";: Next i: Debug.Print ""
pii(0) = 0.364091: pii(1) = 0.185659: pii(2) = 0.139381: pii(3) = 0.100571: pii(4) = 0.070432: pii(5) = 0.139865
If Test = 8 Then pii(0) = 0.324652: pii(1) = 0.182617: pii(2) = 0.14267: pii(3) = 0.106645: pii(4) = 0.077147: pii(5) = 0.166269

X2obs = 0
For i = 0 To kk
   X2obs = X2obs + ((nu(i) - nn * pii(i)) * (nu(i) - nn * pii(i))) / (nn * pii(i))
Next i

' incomplete gamma function
igamc kk / 2, X2obs / 2, p
Debug.Print "NIST k = "; kk; " n = "; nn; "X2obs = "; X2obs;
Debug.Print " igamc = p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 8 Then Cells(5, 2).Value = p

Me29:
Name$ = "Test 2.9"
Debug.Print "Test 2.9 Maurer’s “Universal Statistical” Test page 2.20 "
l = 2: Q = 4:
Q = Int(n1 / 5)
kk = Int(n1 / l) - Q
Linit = l * Q: Ltest = kk * l
Debug.Print "Nist n ="; n1; "L = "; l; "Q = "; Q; "K = "; kk;
Debug.Print "L Initialization"; Linit; "L Test"; Ltest
cnt = 0
For i = 0 To 3: TT(i) = 0: Next i
For i = 1 To Linit Step l
   cnt = cnt + 1
   vdec = 0
   vbin = Val(Mid$(Eps, i, l))
   If vbin >= 10 Then vdec = 2: vbin = vbin - 10
   vdec = vdec + vbin
   TT(vdec) = cnt
   If Test = 9 Then Debug.Print cnt, vdec
Next i
tot = 0
For i = Linit + 1 To Ltest + Linit Step l
   cnt = cnt + 1
   vdec = 0
   vbin = Val(Mid$(Eps, i, l))
   If vbin >= 10 Then vdec = 2: vbin = vbin - 10
   vdec = vdec + vbin
   Nlog2 = Log(cnt - TT(vdec)) / Log(2)   ' compare with old
   tot = tot + Nlog2
   TT(vdec) = cnt                      ' Set new value
   If Test = 9 Then Debug.Print "block = "; cnt; vdec; Nlog2; "sum"; tot
Next i
For i = 0 To 3: Debug.Print "i ="; i; "T ="; TT(i);: Next i: Debug.Print ""
fn = tot / kk
Debug.Print "fn ="; fn
Select Case l
Case 2
    ExpV = 1.5374383: Var = 70.36036717
Case 3
    ExpV = 2.4074383: Var = 30        ' ???
Case 4
    ExpV = 3.3074383: Var = 10
Case 5
    ExpV = 4.244383: Var = 3
Case 6
    ExpV = 5.2177052: Var = 2.954
Case 7
    ExpV = 6.1962507: Var = 3.125
Case 8
    ExpV = 7.1836656: Var = 3.238
Case 9
    ExpV = 8.1764248: Var = 3.311
Case 10
    ExpV = 9.1723243: Var = 3.356
Case 11
    ExpV = 10.170032: Var = 3.384
Case 12
    ExpV = 11.168765: Var = 3.401
Case 13
    ExpV = 12.16807: Var = 3.41
Case 14
    ExpV = 13.167693: Var = 3.416
Case 15
    ExpV = 14.167488: Var = 3.419
Case 16
    ExpV = 15.167379: Var = 3.421
End Select
c = 0.7 - 0.8 / l + ((4 + 32 / l) * kk ^ (-3 / l)) / 15
sigma = c * Sqr(Var / kk)
' sigma = 1.338
' Var = kk * (sigma / c) ^ 2
' Debug.Print "Var"; Var
z = Abs((fn - ExpV) / Sqr(2 * sigma))
Erfc z, p
Debug.Print "Nist fn ="; fn; "sigma"; sigma; "ExpV"; ExpV; "z ="; z; "p = Erfc ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 9 Then Cells(5, 2).Value = p

Name$ = "Test 2.10"
Debug.Print "Test 2.10 Linear Complexity Test page 2.24 "
If Test <> 10 And n1 < 2000 Then GoTo Me211

kk = 6
' ll = 4
mm = 13
nn = Int(n1 / mm)
If nn = 0 Then GoTo Me211
For i = 0 To kk: nu(i) = 0: Next i
i = 0
For j = 1 To nn * mm Step mm
    Eps1 = Mid$(Eps, j, mm)
    i = i + 1
    ll = 1
    ' icnt = 0
    Match = 0
    Do
        ll = ll + 1
        Mask = Mid$(Eps1, 1, ll)
        ' Debug.Print "Eps1 = "; Eps1; " Mask = "; Mask; " ll = "; ll
        For j1 = 1 To mm - ll
            a1$ = Mid$(Mask, j1, 1): a2$ = Mid$(Mask, j1 + 1, 1)
            If (a1$ = "1" And a2$ = "1") Or (a1$ = "0" And a2$ = "0") Then a3$ = "0" Else a3$ = "1"
            Mask = Mask + a3$
        Next j1
        '  Debug.Print Len(Mask), Len(Eps1), icnt
        If Mask = Eps1 Then Match = 1
        ' Debug.Print "Eps1 = "; Eps1; " Mask = "; Mask; " match = "; Match
    Loop Until Match = 1
    ' Debug.Print "i = "; i; "Eps1 = "; Eps1; " Mask = "; Mask; " ll = "; ll
    mu = mm / 2 + (9 + (-1) ^ (mm + 1)) / 36 - (mm / 3 + 2 / 9) / 2 ^ mm
    tti = -1 ^ mm * (ll - mu) + 2 / 9
    If n1 < 256 Then Debug.Print "i = "; i; "Eps1 = "; Eps1; " ll = "; ll; "mu = "; mu; " Ti = "; tti
If tti > 2.5 Then
    nu(6) = nu(6) + 1
Else
    If tti > 1.5 Then
        nu(5) = nu(5) + 1
    Else
        If tti > 0.5 Then                       ' 1.5 to 0.5
            nu(4) = nu(4) + 1
        Else
            If tti > -0.5 Then                  ' 0.5 to -0.5
                nu(3) = nu(3) + 1
            Else
                If tti > -1.5 Then             ' -0.5 to -1.5
                    nu(2) = nu(2) + 1
                Else
                    If tti > -2.5 Then         ' -1.5 to -2.5
                        nu(1) = nu(1) + 1
                    Else
                        nu(0) = nu(0) + 1
                    End If
                End If
            End If
        End If
    End If
End If
Next j
For i = 0 To kk: Debug.Print "i ="; i; "nu"; nu(i); ",";: Next i: Debug.Print ""

pii(0) = 0.010417: pii(1) = 0.03125: pii(2) = 0.125: pii(3) = 0.5: pii(4) = 0.25: pii(5) = 0.0625: pii(6) = 0.002083
X2obs = 0
For i = 0 To kk
   X2obs = X2obs + ((nu(i) - nn * pii(i)) * (nu(i) - nn * pii(i))) / (nn * pii(i))
Next i

' incomplete gamma function
igamc kk / 2, X2obs / 2, p
Debug.Print "NIST K = "; kk; " N = "; nn; "X2obs = "; X2obs;
Debug.Print " igamc = p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 10 Then Cells(5, 2).Value = p

Me211:
Name$ = "Test 2.11"
Debug.Print "Test 2.11 Serial Test page 2.26 "

m = 3 ' Number of bits tested
If m = 1 Then
    Eps1 = Eps
Else
    Eps1 = Eps + Mid$(Eps, 1, m - 1)
End If
If n1 < 256 Then Debug.Print "Nisp n ="; n1; "Eps1 = "; Eps1
For nbit = 1 To m
  phi2m = 0
  For bit = 0 To 2 ^ nbit - 1  ' value
     value1 = bit
     Mask = ""
     If nbit > 2 Then: If value1 >= 4 Then Mask = Mask + "1": value1 = value1 - 4 Else Mask = Mask + "0":
     If nbit > 1 Then: If value1 >= 2 Then Mask = Mask + "1": value1 = value1 - 2 Else Mask = Mask + "0":
     If value1 >= 1 Then Mask = Mask + "1": value1 = value1 - 1 Else Mask = Mask + "0":
     nuu = 0
     For i = 1 To n1
        If Mid$(Eps1, i, nbit) = Mask Then nuu = nuu + 1
     Next i
     If Test = 11 Then Debug.Print nbit; Mask; " Frequency"; nuu
     phi2m = phi2m + nuu * nuu
  Next bit
  phi2m = phi2m * 2 ^ nbit / n1 - n1
  If nbit = 1 Then phi2m1 = phi2m
  If nbit = 2 Then phi2m2 = phi2m
  If nbit = 3 Then phi2m3 = phi2m
  Debug.Print nbit; "Phi2m ="; phi2m
Next nbit
Debug.Print "Nist n ="; n1; "m ="; m
deltaPhi2m = phi2m3 - phi2m2
' incomplete gamma function
igamc 2 ^ (m - 2), deltaPhi2m / 2, p1
Debug.Print "Nist"; 2 ^ (m - 2); deltaPhi2m; "igamc = p1 ="; p1
pmin = p1
If m > 2 Then
    ' incomplete gamma function
    deltaPhi2m = phi2m3 - 2 * phi2m2 + phi2m1
    igamc 2 ^ (m - 3), deltaPhi2m / 2, p2
    Debug.Print "Nist"; 2 ^ (m - 3); deltaPhi2m; "igamc = p2 ="; p2
    If p2 < pmin Then pmin = p2
Else
    p2 = 1  ' random m=2
End If
If pmin < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If pmin < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = pmin
If pmin < pminx Then pminx = pmin
If Test = 11 Then Cells(5, 2).Value = p1
If Test = 11 Then Cells(5, 3).Value = p2

Name$ = "Test 2.12"
Debug.Print "Test 2.12 Approximate Entropy Test page 2.29 "
m = 3 ' Number of bits tested
If m = 1 Then
    Eps1 = Eps
Else
    Eps1 = Eps + Mid$(Eps, 1, m)
End If
If n1 < 256 Then Debug.Print "Nisp Eps1 = "; Eps1
For nbit = m To m + 1
  Phim = 0
  For bit = 0 To 2 ^ nbit - 1  ' value
     value1 = bit
     Mask = ""
     If nbit > 3 Then: If value1 >= 8 Then Mask = Mask + "1": value1 = value1 - 8 Else Mask = Mask + "0":
     If nbit > 2 Then: If value1 >= 4 Then Mask = Mask + "1": value1 = value1 - 4 Else Mask = Mask + "0":
     If nbit > 1 Then: If value1 >= 2 Then Mask = Mask + "1": value1 = value1 - 2 Else Mask = Mask + "0":
     If value1 >= 1 Then Mask = Mask + "1": value1 = value1 - 1 Else Mask = Mask + "0":
     nuu = 0
     For i = 1 To n1
        If Mid$(Eps1, i, nbit) = Mask Then nuu = nuu + 1
     Next i
     C3m = nuu / n1
     If Test = 12 Then Debug.Print nbit; Mask; " Frequency"; nuu; "C3m"; C3m
     If C3m <> 0 Then Phim = Phim + C3m * Log(C3m)
  Next bit
  Debug.Print nbit; "Phi ="; Phim
  If nbit = m Then Phim3 = Phim
  If nbit = m + 1 Then Phim4 = Phim
Next nbit
ApEnm = Phim3 - Phim4
kappa2 = 2 * n1 * (Log(2) - ApEnm)
' incomplete gamma function
igamc 2 ^ (m - 1), kappa2 / 2, p
Debug.Print "Nist n ="; n1; "m ="; m
Debug.Print 2 ^ (m - 1); "ApEn(3) = "; ApEnm; "Kappa2 = "; kappa2; "igamc = p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 12 Then Cells(5, 2).Value = p

Name$ = "Test 2.13"
Debug.Print "Test 2.13 Cumulative Sums (Cusum) Test page 2.31 "
Mode = 0
Debug.Print "Nist  z = "; MaxSn; "mode ="; Mode                  ' See Test 2.1
z = MaxSn
k1 = (-n1 / z + 1) / 4: k2 = (n1 / z - 1)
k3 = (-n1 / z - 3) / 4: k4 = (n1 / z - 1)
Debug.Print "k1 ="; k1; "k2 ="; k2; "k3 ="; k3; "k4 ="; k4
p = 0
Phif 4 * k1 + 1, 4 * k1 - 1, z, n1, p1
p = p + p1
Phif 4 * k2 + 1, 4 * k2 - 1, z, n1, p1
p = p + p1
Phif 4 * k3 + 3, 4 * k3 + 1, z, n1, p1
p = p + p1
Phif 4 * k4 + 3, 4 * k4 + 1, z, n1, p1
p = p + p1
p = 1 - p
Debug.Print "Nist n ="; n1; "p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p
If Test = 13 Then Cells(5, 2).Value = p

Name$ = "Test 2.14"
Debug.Print "Test 2.14 Random Excursions Test page 2.33 "
Debug.Print "Nist  z = "; MaxSn; " (zero crossings) jrw = "; jrw;
If n1 < 256 Then Debug.Print "Eps = "; Eps Else Debug.Print ""
' See Test 2.1
z = MaxSn
Sn = 0
MaxSn = 0                                             ' Test 2.13
'  Tabel page 3.24
picy(0, 1) = 0.5: picy(1, 1) = 0.25: picy(2, 1) = 0.125: picy(3, 1) = 0.0625: picy(4, 1) = 0.0312: picy(5, 1) = 0.0312
picy(0, 2) = 0.75: picy(1, 2) = 0.0625: picy(2, 2) = 0.0469: picy(3, 2) = 0.0352: picy(4, 2) = 0.0264: picy(5, 2) = 0.0791
picy(0, 3) = 0.8333: picy(1, 3) = 0.0278: picy(2, 3) = 0.0231: picy(3, 3) = 0.0193: picy(4, 3) = 0.0161: picy(5, 3) = 0.0804
picy(0, 4) = 0.875: picy(1, 4) = 0.156: picy(2, 4) = 0.0137: picy(3, 4) = 0.012: picy(4, 4) = 0.0105: picy(5, 4) = 0.0733
picy(0, 5) = 0.9: picy(1, 5) = 0.01: picy(2, 5) = 0.009: picy(3, 5) = 0.0081: picy(4, 5) = 0.0073: picy(5, 5) = 0.0656
picy(0, 6) = 0.9167: picy(1, 6) = 0.0069: picy(2, 6) = 0.0064: picy(3, 6) = 0.0058: picy(4, 6) = 0.0053: picy(5, 6) = 0.0588
picy(0, 7) = 0.9286: picy(1, 7) = 0.0051: picy(2, 7) = 0.0047: picy(3, 7) = 0.0044: picy(4, 7) = 0.0041: picy(5, 7) = 0.0531
jrw = 0                                               ' Test 2.14   Sn = 0 zero crossings
For i = 1 To 14: state(i) = 0: Next i
For k = 0 To 5
    For i = 1 To 14: nucy(k, i) = 0: Next i
Next k
For i = 1 To n1
    a$ = Mid$(Eps, i, 1)
    If a$ = "1" Then in1(i) = 1 Else in1(i) = -1
    Sn = Sn + in1(i)
    '  If Test = 14 Then Debug.Print i; Sn
    If Sn = 0 Then
        jrw = jrw + 1                      ' Test 2.14
        For j = 1 To 14: nucy(0, j) = nucy(0, j) + 1: Next j
        For j = 1 To 14:
           If state(j) > 0 Then nucy(state(j), j) = nucy(state(j), j) + 1: nucy(0, j) = nucy(0, j) - 1
        Next j
        If Test = 14 Then
            Debug.Print "cycle "; jrw;
            For j = 1 To 14: Debug.Print j; state(j);: Next j
            Debug.Print
        End If
        For j = 1 To 14: state(j) = 0: Next j
    Else
       If Sn > 0 Then
           pnt = 7 + Sn
           If pnt > 14 Then pnt = 14
           If state(pnt) < 5 Then state(pnt) = state(pnt) + 1
       Else
           pnt = 8 + Sn
           If pnt < 0 Then pnt = 0
           If state(pnt) < 5 Then state(pnt) = state(pnt) + 1
       End If
    End If
    If Abs(Sn) > MaxSn Then MaxSn = Abs(Sn)           ' Test 2.13
Next i
jrw = jrw + 1                                         ' Test 2.14  At end Sn = 0
        If Test = 14 Then
            Debug.Print "cycle "; jrw;
            For j = 1 To 14: Debug.Print j; state(j);: Next j
            Debug.Print
        End If
        
        For j = 1 To 14: nucy(0, j) = nucy(0, j) + 1: Next j
        For j = 1 To 14:
           If state(j) > 0 Then nucy(state(j), j) = nucy(state(j), j) + 1: nucy(0, j) = nucy(0, j) - 1
        Next j
        
        Debug.Print jrw;
        For j = 1 To 14:
           If j <= 7 Then statex = -8 + j Else statex = j - 7
        Next j
        Debug.Print
        For k = 0 To 5
        Debug.Print "# of C "; k;
        For j = 1 To 14:
           If j <= 7 Then statex = -8 + j Else statex = j - 7
           Debug.Print statex; nucy(k, j);:
        Next j
        Debug.Print
        Next k

pmin = 1
For x = 1 To 7
    kappa2 = 0
    For k = 0 To 5
        If x > 0 Then j = x + 7
        fact1 = nucy(k, j) - jrw * picy(k, x)
        kappa2 = kappa2 + fact1 * fact1 / (jrw * picy(k, x))
    Next k
    ' incomplete gamma function
    igamc k / 2, kappa2 / 2, p
    pval(x) = p
    Debug.Print "Nist n ="; n1;
    Debug.Print "x = "; x; "Kappa2 = "; kappa2; "igamc = p ="; p
    If p < pmin Then pmin = p
Next x

If pmin < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If pmin < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = pmin
If pmin < pminx Then pminx = pmin
If Test = 14 Then Cells(5, 2).Value = pmin

Name$ = "Test 2.15"
Debug.Print "Test 2.15 Random Excursions Variant Test page 2.38 "
Debug.Print "Nist  z = "; MaxSn; " (zero crossings) jrw = "; jrw;      ' See Test 2.1
If n1 < 256 Then Debug.Print "Eps = "; Eps Else Debug.Print ""
' Uses 18 states
Sn = 0
For i = 1 To 18: state(i) = 0: Next i
For i = 1 To n1
    a$ = Mid$(Eps, i, 1)
    If a$ = "1" Then in1(i) = 1 Else in1(i) = -1
    Sn = Sn + in1(i)
    If Sn = 0 Then
    Else
       ' Debug.Print i, Sn
       If Sn > 0 Then
           pnt = 9 + Sn
           If pnt > 18 Then pnt = 18
           state(pnt) = state(pnt) + 1
       Else
           pnt = 10 + Sn
           If pnt < 0 Then pnt = 0
           state(pnt) = state(pnt) + 1
       End If
    End If
Next i
        
        For j = 1 To 18:
           If j <= 9 Then statex = -10 + j Else statex = j - 9
           Debug.Print statex; state(j); ",";:
        Next j
        Debug.Print
pmin = 1
For j = 1 To 18
           If j <= 9 Then x = -10 + j Else x = j - 9
           z = Abs(state(j) - jrw) / Sqr(2 * jrw * (4 * Abs(x) - 2))
           Erfc z, p
           Debug.Print j; "x ="; x; "z ="; z; "p ="; p
           If p < pmin Then pmin = p: xmin = x
Next j
If pmin < 0.01 Then Debug.Print "Non Random pmin = "; pmin; "x = "; xmin: errorc = errorc + 1 Else Debug.Print "Random"
If pmin < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = pmin
If pmin < pminx Then pminx = pmin
If Test = 15 Then Cells(5, 2).Value = pmin

Name$ = "Test 3"
Debug.Print "Test 3 Special Normal Distribution Test "
n2 = 30: n22 = n2 / 2
For i = 1 To n2: NDT(i) = 0: Next i
For i = 1 To n1
    a$ = Mid$(Eps, i, 1)
    If i = 1 Then
        If a$ = "1" Then cond = 1 Else cond = -1
    Else
        If a$ = "1" Then
           If cond > 0 Then
               If cond < n22 Then cond = cond + 1
           Else
               NDT(n22 + cond) = NDT(n22 + cond) + 1
               cond = 1
           End If
        Else
           If cond < 0 Then
               If cond > -n22 Then cond = cond - 1
           Else
               NDT(n22 + cond) = NDT(n22 + cond) + 1
               cond = -1
           End If
        End If
    End If
Next i
NDT(n22 + cond) = NDT(n22 + cond) + 1
nn = n1 / 2: nx = 1: bx = 1
For j = 1 To 100
    ns = nx
    bxold = bx
    If ns > 1 Then bx = 1: ns = ns - 1 Else bx = ns: ns = 0
    If ns > 1 Then bx = bx + 2 / 2: ns = ns - 1 Else bx = bx + ns / 2: ns = 0
    If ns > 1 Then bx = bx + 3 / 4: ns = ns - 1 Else bx = bx + ns / 4: ns = 0
    If ns > 1 Then bx = bx + 4 / 8: ns = ns - 1 Else bx = bx + ns / 8: ns = 0
    If ns > 1 Then bx = bx + 5 / 16: ns = ns - 1 Else bx = bx + ns / 16: ns = 0
    If ns > 1 Then bx = bx + 6 / 32: ns = ns - 1 Else bx = bx + ns / 32: ns = 0
    If ns > 1 Then bx = bx + 7 / 64: ns = ns - 1 Else bx = bx + ns / 64: ns = 0
    If ns > 1 Then bx = bx + 8 / 128: ns = ns - 1 Else bx = bx + ns / 64: ns = 0
    bx = (bx + bxold) / 2
    nx = Log(nn / bx) / Log(2)
    ' Debug.Print j; "bx"; bx; "nx"; nx
Next j
istr = 14 - Int(nx) - 2: iend = 16 + Int(nx) + 2
Debug.Print "sum"; bx; "power nx"; nx; "length n"; n1
kappa2 = 0
For i = 1 To n2
    If i > n22 Then
        a$ = ""
        For j = n22 + 1 To i: a$ = a$ + "1": Next j
        Power = nx + n22 + 1 - i
        fact1 = 2 ^ Power
    Else
        If i < n22 Then
            a$ = ""
            For j = i To 14: a$ = a$ + "0": Next j
            Power = nx + i + 1 - n22
            fact1 = 2 ^ Power
        End If
    End If
    If i <> n22 Then kappa2 = kappa2 + (NDT(i) - fact1) * (NDT(i) - fact1) / fact1
    If NDT(i) > 0 Or (i >= istr And i <= iend And i <> n22) Then Debug.Print "i"; i - 15; a$; "  Norm Dist"; fact1; " n = "; NDT(i); "power"; Power
Next i
kappa2 = kappa2
Debug.Print "Kappa2 ="; kappa2;
' incomplete gamma function
igamc n22 / 2, kappa2 / 2, p
Debug.Print "igamc = p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p

Name$ = "Test 3.1"
Debug.Print "Test 3.1 Special Mask test"
mask1$(0) = "000": mask1$(1) = "001": mask1$(2) = "010": mask1$(3) = "011": mask1$(4) = "100": mask1$(5) = "101": mask1$(6) = "110": mask1$(7) = "111"
l = Len(mask1$(0))
For i = 0 To 8: cnt1(i) = 0: cnt2(i) = 0: cond1(i) = 0: Next i
For i = 1 To n1 Step l
   For j = 0 To 7
   If Mid$(Eps, i, l) = mask1$(j) Then
      cnt1(j) = cnt1(j) + 1: cond1(j) = cond1(j) + 1
      If cond1(j) > 1 Then cnt2(j) = cnt2(j) + 1
   Else
      cond1(j) = 0
   End If
   Next j
Next i

predict = n1 / (8 * 3)
For j = 0 To 7
   kappa2 = kappa2 + (cnt1(j) - predict) * (cnt1(j) - predict) / predict
   Debug.Print "Mask = "; mask1$(j); Int(predict); "single cnt = "; cnt1(j); Int(predict / 8); "double cnt = "; cnt2(j)
Next j
kappa2 = kappa2 / 8
Debug.Print "Kappa2 ="; kappa2;
' incomplete gamma function
igamc 8 / 2, kappa2 / 2, p
Debug.Print "igamc = p ="; p
If p < 0.01 Then Debug.Print "Non Random": errorc = errorc + 1 Else Debug.Print "Random"
If p < 0.01 Then Cells(3 + errorc, 3).Value = Name$: Cells(3 + errorc, 4).Value = "Non Rnd": Cells(3 + errorc, 5).Value = p
If p < pminx Then pminx = p

If errorc = 0 Then Cells(4, 3).Value = "No error": Cells(4, 4).Value = "Random"

If Test <= 0 Or Test >= 16 Then Cells(5, 1).Value = "p min": Cells(5, 2).Value = pminx

End Sub


Back to my home page Contents of This Document